package liu.de.web;

import liu.de.dto.TaskDTO;
import liu.de.service.AsyncTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liude
 */
@RestController
@Slf4j
public class TaskController {

    private final AsyncTaskService asyncTaskService;
    @Autowired
    ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    public TaskController(AsyncTaskService asyncTaskService) {
        this.asyncTaskService = asyncTaskService;
    }

    @PostMapping("/tasks")
    public String addTask(@RequestBody TaskDTO taskDTO) {
        asyncTaskService.submit(taskDTO);

//        log.info("taskExecutor: getActiveCount={}, getPoolSize={}", taskExecutor.getActiveCount(), taskExecutor.getPoolSize());

        return "task submitted";
    }
}
